package giro.general.adapter;

import giro.general.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StagesAdapter extends BaseAdapter {

	private JSONArray mStagesList;
	private Context mContext;

	public StagesAdapter(Context context, JSONArray stagesArray) {
		mContext = context;
		mStagesList = stagesArray;
	}

	public int getCount() {
		if (mStagesList != null) {
			return mStagesList.length();
		}
		return 0;
	}

	public Object getItem(int position) {
		if (mStagesList != null) {
			try {
				return mStagesList.get(position);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout view = (LinearLayout) convertView;

		if (convertView == null) {
			view = (LinearLayout) ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.stages_item, null);
		}

		TextView stageNumber = (TextView) view.findViewById(R.id.stage_number);
		TextView stageName = (TextView) view.findViewById(R.id.stage_name);
		TextView stageKm = (TextView) view.findViewById(R.id.stage_km);
		TextView stageData = (TextView) view.findViewById(R.id.stage_data);
		stageNumber.setTextColor(mContext.getResources().getColor(R.color.blue));
		stageName.setTextColor(mContext.getResources().getColor(R.color.blue));
		stageKm.setTextColor(mContext.getResources().getColor(R.color.blue));
		stageData.setTextColor(mContext.getResources().getColor(R.color.blue));

		JSONObject o = null;
		try {
			o = (JSONObject) mStagesList.get(position);
		} catch (JSONException e1) {
			e1.printStackTrace();
		}

		ImageView stageImage = (ImageView) view.findViewById(R.id.stage_image);
		try {

			if (position == 3 || position == 16) {
				stageImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));
				stageKm.setVisibility(View.GONE);
			} else {

				String altimetriaFile = "";
				try {
					altimetriaFile = o.getString("altimetria");
				} catch (JSONException e) {
					e.printStackTrace();
				}

				int nameResourceID = mContext.getResources().getIdentifier(altimetriaFile + "_little", "drawable",
						mContext.getApplicationInfo().packageName);
//				stageImage.setImageDrawable(mContext.getResources().getDrawable(nameResourceID));
				Options options = new Options();
				options.inSampleSize = 1;
				stageImage
						.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), nameResourceID, options));
			}
		} catch (NotFoundException e1) {
			e1.printStackTrace();
		}

		try {
			String tipoStage = o.getString("tipo");
			if (tipoStage.equals("r")) {
				stageNumber.setText(mContext.getResources().getString(R.string.Riposo_Stage));
				stageName.setText(o.getString("data"));
				stageKm.setText("");
				stageData.setText("");
				stageImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.sleep));
			} else {
				stageNumber.setText(mContext.getResources().getString(R.string.Stage_Stage) +" " + o.getString("tappa"));
				stageName.setText(o.getString("nome"));
				stageKm.setText(" - " + o.getString("lunghezza") + " km");
				stageData.setText(o.getString("data"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return view;
	}
}
